package com.qf.ebook.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ebook.entity.EbookEntry;
import com.qf.ebook.exception.EbookException;
import com.qf.ebook.mapper.EbookEntryMapper;
import com.qf.ebook.service.EbookEntryService;
import com.qf.ebook.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.transform.Result;
import java.util.List;

@Service
@Slf4j
public class EbookEntryServiceImpl implements EbookEntryService {

//    private Logger log = LoggerFactory.getLogger(EbookEntryServiceImpl.class);

    @Autowired
    private EbookEntryMapper entryMapper;

    @Override
    public ResultVO findByPage(Integer page, Integer size, Integer type) {
        //1. 分页
        PageHelper.startPage(page,size);
        //2. 查询
        List<EbookEntry> data = entryMapper.findByType(type);
        //3. 封装PageInfo
        PageInfo<EbookEntry> pageInfo = new PageInfo<>(data);
        //4. 封装ResultVO
        ResultVO vo = new ResultVO();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());
        //5. 返回resultVO对象
        return vo;
    }


    @Override
    @Transactional
    public void save(EbookEntry ebookEntry) {
        //1. 调用mapper保存数据
        log.info("【电子图书信息】 添加ebookEntry到数据库!!");
        Integer count = entryMapper.save(ebookEntry);

        //2. 判断保存是否成功
        if(count != 1){
            // 保存有问题
            log.error("【电子图书信息】 添加电子图书失败！！ ebookEntry = {}",ebookEntry);
            throw new EbookException(22,"【电子图书信息】 添加电子图书失败！！");
        }

    }
}
